package com.yst.gateway.monitor.services.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yst.gateway.monitor.common.utils.DateUtils;
import com.yst.gateway.monitor.common.utils.LoginUserCache;
import com.yst.gateway.monitor.dao.SysUsersMapper;
import com.yst.gateway.monitor.models.SysUsers;
import com.yst.gateway.monitor.services.SysUserService;

/**
 * 前置系统用户管理业务实现类
 * @author sheungxin
 *
 */
@Service
@Scope("prototype")
public class SysUserServiceImpl implements SysUserService{

	@Autowired
	private SysUsersMapper sysUsersMapper;
	
	@Override
	public PageInfo<SysUsers> getByPage(int pageNum, int pageSize,
			Map<String, Object> paramMap) {
		PageHelper.startPage(pageNum, pageSize);
		return new PageInfo<SysUsers>(sysUsersMapper.selectAll(paramMap));
	}

	@Override
	public boolean add(SysUsers t) {
		boolean flag=true;
		if(getByName(t.getSysUserName())==null){
			t.setSysUserId(t.getSysUserId());
			t.setSysUserCreator(LoginUserCache.getUser()==null?t.getSysUserId():LoginUserCache.getUser().getSysUserId());
			t.setSysUserCtime(DateUtils.getNowTime());
			t.setSysUserPsw("");
//			t.setSysUserPsw(new Md5PasswordEncoder().encodePassword(t.getSysUserPsw(), t.getSysUserId()));
			t.setSysUserStatus(1);
			t.setSysUserDel(1);
			if(sysUsersMapper.insert(t)>0){
				
			}else{
				flag=false;
			}
		}else{
			flag=false;
		}
		return flag;
	}

	@Override
	public boolean update(SysUsers t) {
		boolean flag=true;
		/*if(t.getSysUserName()!=null){
			t.setSysUserName(null);
		}
		if(t.getSysUserCtime()!=null){
			t.setSysUserCtime(null);
		}
		if(t.getSysUserCreator()!=null){
			t.setSysUserCreator(null);
		}
		if(t.getSysUserStatus()!=null){
			t.setSysUserStatus(null);
		}
		if(t.getSysUserDel()!=null){
			t.setSysUserDel(null);
		}*/
		t.setSysUserModifier(LoginUserCache.getUserId());
		t.setSysUserUtime(DateUtils.getNowTime());
		if(StringUtils.isNotBlank(t.getSysUserPsw())){
			t.setSysUserPsw(new Md5PasswordEncoder().encodePassword(t.getSysUserPsw(), t.getSysUserId()));
		}
		if(sysUsersMapper.updateByPrimaryKey(t)>0){
			
		}else{
			flag=false;
		}
		return flag;
	}

	@Override
	public SysUsers getById(String id) {
		return sysUsersMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<SysUsers> getByMap(Map<String, Object> paramMap) {
		return sysUsersMapper.selectAll(paramMap);
	}

	@Override
	public boolean updateStatusById(String id) {
		boolean flag=false;
		SysUsers t=getById(id);
		if(t!=null){
			SysUsers upUser=new SysUsers();
			upUser.setSysUserId(id);
			upUser.setSysUserStatus(t.getSysUserStatus()==0?1:0);
			upUser.setSysUserUtime(DateUtils.getNowTime());
			upUser.setSysUserModifier(LoginUserCache.getUserId());
			flag=sysUsersMapper.updateByPrimaryKey(upUser)>0?true:false;
		}
		return flag;
	}

	@Override
	public boolean delById(String id) {
		SysUsers upUser=new SysUsers();
		upUser.setSysUserId(id);
		upUser.setSysUserDel(0);
		upUser.setSysUserUtime(DateUtils.getNowTime());
		upUser.setSysUserModifier(LoginUserCache.getUserId());
		return sysUsersMapper.updateByPrimaryKey(upUser)>0?true:false;
	}

	@Override
	public SysUsers getByName(String name) {
		return sysUsersMapper.selectByName(name);
	}

}
